package com.campus.controller;

import com.campus.common.ResponseResult;
import com.campus.exception.BusinessException;
import com.campus.model.User;
import com.campus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @desc: 用户控制器类，用于处理用户相关的HTTP请求
 * @author: hjm
 * @date: 2024/11/22
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    /**
     * 构造函数，注入 UserService 依赖
     *
     * @param userService 用户服务对象
     */
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户登录接口
     *
     * @param loginRequest 包含用户名和密码的请求体
     * @return 包含Token的ResponseResult，如果登录失败则返回错误信息
     */
    @PostMapping("/login")
    public ResponseResult<?> login(@RequestBody Map<String, String> loginRequest) {
        String username = loginRequest.get("username");
        String password = loginRequest.get("password");

        try {
            String token = userService.login(username, password);
            System.out.println("用户登录成功，用户名: " + username);
            return ResponseResult.ok(Map.of("token", token), "登录成功");
        } catch (BusinessException e) {
            System.out.println("用户登录失败，用户名: " + username + " - " + e.getMessage());
            return ResponseResult.error(HttpStatus.UNAUTHORIZED, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "服务器内部错误");
        }
    }

    /**
     * 创建用户接口
     *
     * @param user 用户对象
     * @return 创建的用户对象
     */
    @PostMapping("/create")
    public ResponseResult<User> createUser(@RequestBody User user) {
        User createdUser = userService.createUser(user);
        return ResponseResult.ok(createdUser, "创建用户成功");
    }

    /**
     * 根据用户ID获取用户信息接口
     *
     * @param id 用户唯一标识符
     * @return 用户对象
     */
    @GetMapping("/{id}")
    public ResponseResult<User> getUserById(@PathVariable("id") Long id) {
        User user = userService.getUserById(id);
        return user != null ? ResponseResult.ok(user, "查询成功") : ResponseResult.error(HttpStatus.NOT_FOUND, "用户不存在");
    }

    /**
     * 根据用户名获取用户信息接口
     *
     * @param username 用户名
     * @return 用户对象
     */
    @GetMapping("/username/{username}")
    public ResponseResult<User> getUserByUsername(@PathVariable("username") String username) {
        User user = userService.getUserByUsername(username);
        return user != null ? ResponseResult.ok(user, "查询成功") : ResponseResult.error(HttpStatus.NOT_FOUND, "用户不存在");
    }

    /**
     * 获取系统中所有用户的列表接口
     *
     * @return 用户列表
     */
    @GetMapping("/list")
    public ResponseResult<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseResult.ok(users, "查询成功");
    }
}